//
//  LOLEquipDetailViewController.m
//  LOLXXbox
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 Grasstrio. All rights reserved.
//

#import "LOLEquipDetailViewController.h"
#import "LOLFirstDetailEquipCell.h"
#import "LOLSecondDetailEquipCell.h"
#import "LOLThirdDetailEquipCell.h"
#import <AFNetworking.h>
#import "LSZNetWorkTools.h"
#import "LOLEquipDetailModel.h"
@interface LOLEquipDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) LOLEquipDetailModel *equipDetailModel;


@end

@implementation LOLEquipDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTableView];
    [self getData];
    
}

- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, - 32, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[LOLFirstDetailEquipCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[LOLSecondDetailEquipCell class] forCellReuseIdentifier:@"cell2"];
    [self.tableView registerClass:[LOLThirdDetailEquipCell class] forCellReuseIdentifier:@"cell3"];
    [self.view addSubview:_tableView];
 
    
}
- (void)getData{
    [LSZNetWorkTools getWithUrl:[NSString stringWithFormat:@"http://lolbox.duowan.com/phone/apiItemDetail.php?id=%@",self.idId] parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dic = result;
//        NSLog(@"%@",result);
        self.equipDetailModel = [[LOLEquipDetailModel alloc] initWithDataSource:dic];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } fail:^(NSError *error) {
        
    }];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        LOLFirstDetailEquipCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.detailModel = self.equipDetailModel;
        return cell;
    }else if (indexPath.section == 1){
        LOLSecondDetailEquipCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell2"];
        cell.detailModel = self.equipDetailModel;
        return cell;
    }else if (indexPath.section == 2){
        LOLThirdDetailEquipCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell3"];
        cell.array = [self.equipDetailModel.need componentsSeparatedByString:@","];
        return cell;
    }else{
        LOLThirdDetailEquipCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell3"];
        cell.array = [self.equipDetailModel.compose componentsSeparatedByString:@","];
        return cell;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 100;
    }else if (indexPath.section == 1){
        LOLSecondDetailEquipCell *cell = (LOLSecondDetailEquipCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        [cell layoutSubviews];
        return [cell getHeight];
    }else{
        return 70;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }else if (section == 1){
        return @"物品属性";
    }else if (section == 2){
        return @"合成需求";
    }else{
        return @"可合成";
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 32;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
